package com.cn.webspider;

import com.cn.util.BeanUtilsIgnoreNull;
import com.cn.util.JdbcUtil;
import org.springframework.beans.BeanUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Arrays;
import java.util.List;

public class SaveDouBan implements Pipeline {

    @Override
    public void process(ResultItems resultItems, Task task) {
        DoubanSpider.Subjects douban = resultItems.get("doubans");
        //存第一部分内容
        if (douban != null) {
            douban.getSubjects().stream().forEach(e -> {
                //判断如果存在则跳过，
                if (JdbcUtil.selectAll("select * from douban where id = " + e.getId()).size() == 0) {
                    JdbcUtil.insert(e);
                }
            });
        }
        DoubanSpider.Douban detail = resultItems.get("detail");
        //存第二部分内容
        if (detail != null) {
            List<? extends DoubanSpider.Douban> doubans = JdbcUtil.selectAll(DoubanSpider.Douban.class, detail);
            if (doubans != null) {
                //判断如果存在则跳过，
                String[] nullPropertyNames = BeanUtilsIgnoreNull.getNullPropertyNames(doubans.get(0), "is_new", "palyable");
                List<String> strings = Arrays.asList(nullPropertyNames);
//                strings.add("palyable");
//                strings.add("is_new");
                BeanUtils.copyProperties(doubans.get(0), detail, nullPropertyNames);
                JdbcUtil.update(detail);
            } else {
                JdbcUtil.insert(detail);
            }
        }
    }
}